package com.fairisanddog.controller;


import com.alibaba.fastjson.JSON;

import com.fairisanddog.entity.Cart;
import com.fairisanddog.service.CartService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    @Qualifier("CartServiceImpl")
    private CartService cartService;

    @RequestMapping("/findAllShops")
    @ResponseBody
    public String queryAllShops(HttpServletRequest request, HttpServletResponse response){
        List<Cart> carts = cartService.queryAllShops();
        HttpSession session =request.getSession();
        session.setAttribute("carts",carts);
        if (null == session.getAttribute("carts")){
            return "0";
        }else {
            return "1";
        }

    }

    @RequestMapping("/addCart")
    @ResponseBody
    public  String addShopToCart(@RequestBody Cart cart){
        Cart cart1 = new Cart(cart.getCid(),cart.getCname(),cart.getCprice(),cart.getUid(),cart.getCnum(),cart.getCtype());
        Cart cart2 = cartService.queryShopByCidAndUid(cart.getCid(),cart.getUid());
        if (null == cart2){
            cartService.addShop(cart1);
            return "1";
        }else {

            return "0";
        }



    }
}
